﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;

namespace mamnontuoitho7.backend
{
    public partial class ParentList : System.Web.UI.Page
    {

        private mamnontuoitho7.AppCode.Parent _parent = new mamnontuoitho7.AppCode.Parent();
        protected Product _product = new Product();
        protected General _general = new General();

        protected DataTable _dt;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalParentNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        private String _memCookieSearch = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Request.Cookies[ConstantClass.cooTeacherList_ddlStatus] == null)
                        {
                            HttpCookie cookie = new HttpCookie(ConstantClass.cooParentList_memSearch, Server.UrlEncode("****"));
                            Response.Cookies.Add(cookie);
                            cookie.Expires = DateTime.Now.AddDays(1);

                        }
                        else
                        {
                            try
                            {
                                String[] memSearch = Server.UrlDecode(Request.Cookies[ConstantClass.cooParentList_memSearch].Value).Split('*');
                                txtFullName.Text = memSearch[0];
                                txtMemberName.Text = memSearch[1];
                                txtEmail.Text = memSearch[2];
                                txtPhone.Text = memSearch[3];
                                txtKid.Text = memSearch[4];
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }
                        

                        Paging(txtFullName.Text.Trim(), txtMemberName.Text.Trim(),
                            txtEmail.Text.Trim(), txtPhone.Text.Trim(), txtKid.Text.Trim());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }


         /// <summary>
         /// Search and paging
         /// </summary>
         /// <param name="fullName"></param>
         /// <param name="memberName"></param>
         /// <param name="email"></param>
         /// <param name="phone"></param>
         /// <param name="allKid"></param>
        private void Paging(String fullName, String memberName, String email, String phone, String allKid)
        {
            _totalParentNumber = _parent.TotalParentOfOneSearchTimes(fullName, memberName, email, phone, allKid).GetValueOrDefault();
            _totalPages = _general.Divide(_totalParentNumber, ConstantClass._pageStepParent);
            _totalBars = _general.Divide(_totalPages, ConstantClass._barStepParent);

            if (Request.QueryString["page"] != null)// khi bấm vào các ô số
            {
                string temp = Request.QueryString["page"].ToString();
                Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                if (temp.Length <= 4 && match.Success)
                {
                    _pageflag = Convert.ToInt16(temp);// trang hiện hành

                    if (_pageflag <= _totalPages)
                    {
                        _currentbar = _general.Divide(_pageflag, ConstantClass._barStepParent);
                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                    }
                    else { _pageflag = 1; }

                }
            }

            if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
            {
                _currentbar = 1;
                _pageflag = 1;
                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
            }

            if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
            {
                _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                if (_currentbar < _totalBars)
                {
                    _currentbar += 1;
                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                }
                _pageflag = (_currentbar - 1) * ConstantClass._barStepParent + 1;

            }

            if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
            {
                _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                if (_currentbar > 1)
                {
                    _currentbar -= 1;
                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                }
                _pageflag = (_currentbar - 1) * ConstantClass._barStepParent + 1;
            }

            _dt = _parent.GetParentPaged(_pageflag, ConstantClass._pageStepParent, fullName, memberName, email, phone, allKid);

            _beginPage = (_currentbar - 1) * ConstantClass._barStepParent + 1;
            _endPage = _currentbar * ConstantClass._barStepParent;

            if (_endPage > _totalPages)
            {
                _endPage = _totalPages;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
            _pageflag = 1;
            _currentbar = 1;

            Paging(txtFullName.Text.Trim(), txtMemberName.Text.Trim(), txtEmail.Text.Trim(),
                txtPhone.Text.Trim(), txtKid.Text.Trim());

            _memCookieSearch = txtFullName.Text.Trim() + "*" + txtMemberName.Text.Trim() + "*" + txtEmail.Text.Trim() + "*" +
                        txtPhone.Text.Trim() + "*" + txtKid.Text.Trim();

            if (Request.Cookies[ConstantClass.cooTeacherList_ddlStatus] == null)
            {
                HttpCookie cookie = new HttpCookie(ConstantClass.cooParentList_memSearch, Server.UrlEncode(_memCookieSearch));
                Response.Cookies.Add(cookie);
                cookie.Expires = DateTime.Now.AddDays(1);

            }
            else
            {
                Response.Cookies[ConstantClass.cooParentList_memSearch].Value = Server.UrlEncode(_memCookieSearch);
            }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
    }
}